package com.win.app;

import java.io.IOException;
import java.security.DigestException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.li.user.JsApiSignInfo;
import com.li.utils.WeixinPayUtil;
import com.li.utils.WxConfigure;
import com.li.utils.WxTools;

/**
 * Servlet implementation class WeixinServlet
 */
@WebServlet("/WeixinServlet")
public class WeixinServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WeixinServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String random = request.getParameter("r");
		String path = ""; 
		if(random==null || "".equals(random)){
			path = "http://li.hk1.mofasuidao.cn/winImg/WeixinServlet?from=singlemessage"; 
		}else{
			path = "http://li.hk1.mofasuidao.cn/winImg/WeixinServlet?r="+random+"&from=singlemessage";
			
		}
		//String path = "http://li.hk1.mofasuidao.cn/winImg/WeixinServlet?r="+random+"&from=singlemessage";
		System.out.println(path);
		try {
			JsApiSignInfo jsApi = WeixinPayUtil.getJSAPISign(WxConfigure.getJsapi_ticket(), path, WxTools.getRandomString(16), WxTools.getTimeStamp());
			request.setAttribute("jsApi", jsApi);
			request.setAttribute("appid", WxConfigure.getAppid());
			request.setAttribute("r", random);
		} catch (DigestException | IllegalAccessException e) {
			e.printStackTrace();
		}
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
